@extends('cms/system/admin/template/theme_default/common')
@section('page-body')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form layui-form-pane" id="office-enterprise-form" lay-filter="office-enterprise-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属应用</label>
                        @if(isset($app) && $app)
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$app['id'] ?? 0}}" />
                                <input class="layui-input" type="text" value="{{$app['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @else
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$data['app_id'] ?? 0}}" />
                                <input id="app-select" class="layui-input" type="text" value="{{$data['app']['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @endif
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">企业名称</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="name" value="{{$data['name'] ?? ''}}" placeholder="请输入名称" autocomplete="off" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">企业别名</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="alias" value="{{$data['alias'] ?? ''}}" placeholder="请输入别名" autocomplete="off" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <input class="layui-btn" type="button" value="添加" id="submit-create" lay-submit lay-filter="submit-create" />
                            <input class="layui-btn" type="button" value="更新" id="submit-update" lay-submit lay-filter="submit-update" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script type="text/javascript">
        layui.config({
            base: '/layui-admin/'
        }).extend({
            index: 'lib/index'
        }).use(['index'], function () {
            // 选择应用
            layui.$('#app-select').on('click', function () {
                // region 处理已选中 +++++
                var selectValueDom = layui.$('#app-id');
                var selectValue = selectValueDom.val();
                var selectTextDom = layui.$('#app-select');
                // endregion

                var selectLayer = top.layui.layer.open({
                    type: 2,
                    title: '选择',
                    content: layui.setter.mvcLink + '/office/admin_app/select?select_multi=0&select_value=' + selectValue,
                    maxmin: true,
                    area: '500px',
                    maxHeight: '500px',
                    btn: ['确定', '取消'],
                    yes: function (index, parentLayer) {
                        console.log(index, selectLayer)
                        // 取得iframe窗口
                        // var iframe = window[parentLayer.find('iframe')[0]['name']];
                        var iframe = top.layui.$('#layui-layer-iframe' + index)[0];
                        var selectResult = iframe.contentWindow.selectConfirm();
                        if (selectResult.data.length != 1) {
                            top.layui.layer.msg('请正确选择所属题库');
                            return false;
                        }
                        var selectText = '';
                        // 清空已有值，避免再次选择重复问题
                        selectValue = '';
                        layui.$.each(selectResult.data, function (data_k, data_v) {
                            if (data_k > 0) {
                                selectValue += ',';
                                selectText += ',';
                            }
                            selectValue += data_v.id;
                            selectText += data_v.id + '#' + data_v.name;
                        });
                        // 设置值
                        selectValueDom.val(selectValue);
                        selectTextDom.val(selectText);
                        top.layui.layer.close(index);
                    }
                });
            });
            // 创建
            layui.form.on('submit(submit-create)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_enterprise/create?app_id={{$app['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-enterprise-list');
                    }
                });
            });
            // 更新
            layui.form.on('submit(submit-update)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                console.log('表单字段：', formField);
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_enterprise/update?app_id={{$app['id'] ?? 0}}&id={{$data['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-enterprise-list');
                    }
                });
            });
        });
    </script>
@endsection